package com.mike.blog.service.impl;

import com.mike.blog.constant.ApplicationConstant;
import com.mike.blog.entity.Article;
import com.mike.blog.repository.ArticleRepository;
import com.mike.blog.service.ArticleService;
import com.mike.blog.vo.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaozefeng
 * @date 2018-12-23 16:44
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public List<ArticleVO> findAll() {
        List<Article> articles =
                articleRepository.findAll();
        return articles.stream()
                .map(article -> {
                    ArticleVO articleVO = new ArticleVO();
                    articleVO.setTitle(article.getTitle());
                    articleVO.setSubTitle(article.getSubTitle());
                    articleVO.setCreatedTime(article.getCreatedTime().format(ApplicationConstant.DEFAULT_TIME_FORMATTER));
                    articleVO.setContent(article.getContent());
                    return articleVO;
                }).collect(Collectors.toList());
    }
}
